﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Euclidean.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace BOL.Linq.Distances
{
    public static partial class Disimilarity
    {
        /// <summary>
        /// Euclidean = sqrt(b + c)
        /// </summary>
        public static double Euclidean(this IEnumerable<bool> first, IEnumerable<bool> second)
        {
            var bc = first.BinaryCardinality(second);

            return Math.Sqrt(bc.Item2 + bc.Item3);
        }

        public static float Euclidean(this float first, float second)
        {
            return Math.Abs(first - second);
        }

        public static double Euclidean(this double first, double second)
        {
            return Math.Abs(first - second);
        }

        public static decimal Euclidean(this decimal first, decimal second)
        {
            return Math.Abs(first - second);
        }

        public static int Euclidean(this int first, int second)
        {
            return Math.Abs(first - second);
        }

        public static long Euclidean(this long first, long second)
        {
            return Math.Abs(first - second);
        }

        public static float Euclidean(this IEnumerable<float> first, IEnumerable<float> second)
        {
            return (float)Math.Sqrt(first.Zip(second, (f, s) => Math.Pow(f - s, 2.0)).Sum());
        }

        public static double Euclidean(this IEnumerable<double> first, IEnumerable<double> second)
        {
            return Math.Sqrt(first.Zip(second, (f, s) => Math.Pow(f - s, 2.0)).Sum());
        }

        public static double MeanCensoredEuclidean(this IEnumerable<double> first, IEnumerable<double> second)
        {
            var x = first.ToList();
            var y = second.ToList();
            var length = Math.Min(x.Count, y.Count);

            return Math.Sqrt(x.Zip(y, (xc, yc) => xc - yc).Sum(diff => diff * diff) / length);
        }

        public static double Euclidean(this IEnumerable<decimal> first, IEnumerable<decimal> second)
        {
            return Math.Sqrt(first.Zip(second, (f, s) => Math.Pow((double)f - (double)s, 2.0)).Sum());
        }

        public static double Euclidean(this IEnumerable<int> first, IEnumerable<int> second)
        {
            return Math.Sqrt(first.Zip(second, (f, s) => Math.Pow(f - s, 2.0)).Sum());
        }

        public static double Euclidean(this IEnumerable<long> first, IEnumerable<long> second)
        {
            return Math.Sqrt(first.Zip(second, (f, s) => Math.Pow(f - s, 2.0)).Sum());
        }
    }
}
